<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/tld/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Manage Score</title>
<link rel="stylesheet" type="text/css" href="css/FinalProjectCSS.css"/>
<script type="text/javascript" src="validationWithJs.js"></script>
<script type="text/javascript">
	function flyToPage(task) {
		document.forms[0].task.value = task;
		document.forms[0].submit();
	}
	
// 	function flyToPage2() {
// 		document.forms[0].task.value = "listAction";
// 		document.forms[0].submit();
// 	}

	function updateScore() {
		var score = new Array();
		score = document.getElementsByName('score');
		for (var i = 0; i < score.length; i++) {
			if(isNaN(score[i].value)==true){
				alert("Score must be a number");
				return;
			}else if(score[i].value<0 || score[i].value>100){
				alert("Score must be between 0 and 100");
				return;
			}
		}
		var r=confirm("Are you sure want to update score?");
		if (r==true){
			flyToPage('updateScore');
		}
	}
	
	function verified() {
		var r=confirm("Are you sure want to verify score?");
		if (r==true){
			flyToPage('verified');
		}
	}

	function cancel(){
		document.getElementById('insertField').style.display = 'none';
	}

	function showSubjectCombo() {
// 		var batch = document.getElementsByName('searchBatch')[0].value;
// 		var task = 'showSubjectCombo';
// 		$.ajax({
//             type: "POST",
//             url: "/WebFinalProject/Score.do",
//             data: "searchBatch=" + batch + "&task=" + task,
//             success: function(response){
//                 $('#subjectCombo').html(response);
//             },
//             error: function(e){
//                 alert('Error: ' + e);
//             }
//         });
		document.forms[0].task.value = "showSubjectCombo";
		document.forms[0].submit();
	}
	
	function showScoreUpdateField() {
    	var batch = document.getElementsByName('searchBatch')[0].value;
		var subject = document.getElementsByName('searchSubject')[0].value;
		var session = document.getElementsByName('searchSession')[0].value;
		var task = 'showUpdateField';
		if(subject.length!=0 && session.length!=0){
			document.getElementById('insertField').style.display = '';
	        $.ajax({
	            type: "POST",
	            url: "/WebFinalProject/Score.do",
	            data: "searchBatch=" + batch + "&searchSubject=" + subject + "&searchSession=" + session + "&task=" + task,
	            success: function(response){
	                $('#insertField').html(response);
	            },
	            error: function(e){
	                alert('Error: ' + e);
	            }
	        });
		}else{
			document.getElementById('insertField').style.display = 'none';
		}
    }
	
	function showUploadField() {
		var batch = document.getElementsByName('searchBatch')[0].value;
		var subject = document.getElementsByName('searchSubject')[0].value;
		var session = document.getElementsByName('searchSession')[0].value;
		var task = 'showUploadField';
		if(subject.length!=0 && session.length!=0){
			document.getElementById('insertField').style.display = '';
	        $.ajax({
	            type: "POST",
	            url: "/WebFinalProject/Score.do",
	            data: "searchBatch=" + batch + "&searchSubject=" + subject + "&searchSession=" + session + "&task=" + task,
	            success: function(response){
	                $('#insertField').html(response);
	            },
	            error: function(e){
	                alert('Error: ' + e);
	            }
	       	});
		}else{
			document.getElementById('insertField').style.display = 'none';
		}
	}
	
	function showSessionCombo() {
		 var batch = document.getElementsByName('searchBatch')[0].value;
		 var subject = document.getElementsByName('searchSubject')[0].value;
		 var task = 'showSessionCombo';
		 $.ajax({
             type: "POST",
             url: "/WebFinalProject/Score.do",
             data: "searchBatch=" + batch + "&searchSubject=" + subject + "&task=" + task,
             success: function(response){
                 $('#sessionCombo').html(response);
             },
             error: function(e){
                 alert('Error: ' + e);
             }
         });
	}
	
	function uploadScore() {
		var filePath = document.getElementsByName('uploadFile')[0].value;
		var fileName = filePath.substr(filePath.lastIndexOf("\\") + 1);
		
		if(filePath==""){
			alert('Please choose file first');
			return;
		}
		if(checkFile(fileName)){
			document.getElementsByName('fileName')[0].value = fileName;
			flyToPage('uploadScore');
		}else{
			alert('File should be in .xls format');
			return;
		}
	}
	
	function checkScoreBtn() {
		var subject = document.getElementsByName('searchSubject')[0].value;
		var session = document.getElementsByName('searchSession')[0].value;
		if(subject==null || subject==0){
			document.getElementById('manageButton').disabled = true;
			document.getElementById('uploadButton').disabled = true;
		}else {
			document.getElementById('manageButton').disabled = false;
			document.getElementById('uploadButton').disabled = false;
		}
	}
</script>
</head>
<%
	if(session.getAttribute("result") != null)
	{
		String message = session.getAttribute("result").toString();
		%>
			<script type="text/javascript">
				window.alert('<%=message%>');
			</script>
		<%
		session.removeAttribute("result");
	}
%>
<body onload="javascript:showSessionCombo();checkScoreBtn();" onkeydown="javascript:disableF5(event, 'insertScore');">	
	<center>
		<table width="100%" class="bodyTable boxShadow">
		
			<!-- Header -->
			<tr>
				<td colspan="2"><%@include file="/include/Header.jsp"%></td>
			</tr>
		
			<!-- Content -->
			<tr>
				<td rowspan="2" width="200px" class="navigationBox"><%@include file="/include/Navigation.jsp"%></td>
				<td align="left" class="headerBox blueBackground">ACE Management > <a href="javascript:flyToPage('listScore');">ACE Score</a> > Manage Score</td>
			</tr>
			<tr>
				<td align="center"  class="contentStyle"><br><br>
					<html:form action="/Score" method="post" enctype="multipart/form-data">
						<html:hidden property="task" name="scoreForm" />
						<html:hidden property="fileName" name="scoreForm"/>
						<table width="35%" align="center" border="0" class="boxShadow">
							<tr class="headerBox formHeaderBackground">
								<td colspan="3" align="center" class="formListStyle">Manage Score</td>
							</tr>
							<tr>
								<td class="formListStyle rightAlign">Batch</td>
								<td class="formListStyle centerAlign">
			    					<html:select styleClass="comboMedium" name="scoreForm" property="searchBatch" onchange="javascript:flyToPage('showSubjectCombo');" >
						    			<logic:notEmpty name="scoreForm" property="listBatch" >
						    				<html:optionsCollection name="scoreForm" property="listBatch" value="batchId" label="batchName" />
						    			</logic:notEmpty>
						    		</html:select>
			    				</td>
							</tr>
							<tr>
								<td class="formListStyle rightAlign">Subject</td>
								<td class="formListStyle centerAlign">
			    					<html:select styleClass="comboMedium" name="scoreForm" property="searchSubject" onchange="javascript:showSessionCombo();" >
						    			<logic:notEmpty name="scoreForm" property="listSubject" >
						    				<html:optionsCollection name="scoreForm" property="listSubject" value="classId" label="className" />
						    			</logic:notEmpty>
						    			<logic:empty name="scoreForm" property="listSubject">
						    				<html:option value="0">No Subject</html:option>
						    			</logic:empty>
						    		</html:select>
		    					</td>
							</tr>
							<tr>
								<td class="formListStyle rightAlign">Session</td>
								<td class="formListStyle centerAlign">
									<select class="comboMedium" name="searchSession" id="sessionCombo" ></select>
		    					</td>
							</tr>
							<tr align="center">
								<td colspan="3" class="formListStyle">
									<table>
										<tr>
											<td align="right" class="formListStyle">
												<table title="manage score" id="manageButton" class="buttonStyle" style="cursor: pointer;" onclick="javascript:showScoreUpdateField();">
													<tr>
														<td style="vertical-align:middle;"><img src="images/editButton.png"/></td>
														<td style="vertical-align:middle;"><b>Manage Score&nbsp;</b></td>
													</tr>
												</table>
											</td>
											<td align="left" class="formListStyle">			
												<table title="upload score" id="uploadButton" class="buttonStyle" style="cursor: pointer;" onclick="javascript:showUploadField();">
													<tr>
														<td style="vertical-align:middle;"><img src="images/uploadButton.png"/></td>
														<td style="vertical-align:middle;"><b>Upload Score&nbsp;</b></td>
													</tr>
												</table>
											</td>
											<td align="center" class="formListStyle">
												<table title="back" class="buttonStyle" style="cursor: pointer;" onclick="javascript:flyToPage('listScore');">
													<tr>
														<td style="vertical-align:middle;"><img src="images/backButton.png"/></td>
														<td style="vertical-align:middle;"><b>Back</b></td>
													</tr>
												</table>
											</td>
										</tr>
									</table>
								</td>
							</tr>
						</table>
						<br />
						<div id="insertField">
						</div>
					</html:form><br>
				</td>
			</tr>
			
			<!-- Footer -->
			<tr>
				<td align="center" colspan="2" class="headerBox blueBackground">&copy; OME. 2013. All Rights Reserved.</td>
			</tr>
		</table>
	</center>
</body>
</html>